###REPLICATION CODE

##Paper: "Who Settles Disputes? Treaty Design and Trade Attitudes toward the Transatlantic Trade and Investment Partnership (TTIP)"
##Journal: International Organization
##Authors: Hyeonho Hahm, Thomas Koenig, Moritz Osnabruegge and Elena Frech
##Date: February 18, 2019


rm(list = ls())

#Load packages
library(cjoint)
library(foreign)
library(readstata13)
library(nnet)
library(AER)


data = read.dta13("data.dta")


#Prepare labels
data$customs = as.character(data$customs)
data$standards = as.character(data$standards)
data$exceptions = as.character(data$exceptions)
data$dispute = as.character(data$dispute)
data$endorsement = as.character(data$endorsement)

#Create conjoint design
attribute_list = list()
attribute_list[["customs"]] = c("50% of the cases","fully dismissed","remain as they are")
attribute_list[["standards"]] = c("50% of the cases","fully standardized","remain as they are")
attribute_list[["exceptions"]] = c("automobiles","chemicals","cosmetic products","food","industrial machines","no exceptions","pharmaceuticals")
attribute_list[["dispute"]] = c("domestic court","tribunal by national representatives","arbitration court")
attribute_list[["endorsement"]] = c("European Commission","consumer organization","German government","opposition parties")

#Randomization constraints
constraint_list = list()
constraint_list[[1]] = list()
constraint_list[[1]][["standards"]] = c("fully standardized","remain as they are")
constraint_list[[1]][["exceptions"]] = c("automobiles","chemicals","cosmetic products","food","industrial machines","pharmaceuticals") 

#Create design 
ttipdesign = makeDesign(type="constraints", attribute.levels=attribute_list, constraints=constraint_list)
class(ttipdesign)

data$customs = as.factor(data$customs)
data$standards = as.factor(data$standards)
data$exceptions = as.factor(data$exceptions)
data$dispute = as.factor(data$dispute)
data$endorsement = as.factor(data$endorsement)

baselines = list()
baselines$customs = "remain as they are"
baselines$standards = "remain as they are"
baselines$exceptions = "no exceptions"
baselines$dispute = "domestic court"
baselines$endorsement = "European Commission"


##Figure 1: AMCEs (results are in the file "main_results.txt", see subsetlabel: "Without weights")
result = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result)


##Figure 2: AMCE by subgroups (results are in the file "fig2.txt")
data_edu1 = subset(data, high_school==1) # (subsetlabel: "High education")
result_edu1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_edu1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_edu1)

data_edu2 = subset(data, high_school==0) # (subsetlabel: "Low education")
result_edu2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_edu2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_edu2)

data_knowledge1 = subset(data, knowledge==1) # (subsetlabel: "Heard about TTIP")
result_knowledge1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_knowledge1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_knowledge1)

data_knowledge2 = subset(data, knowledge==0) # (subsetlabel: "Not heard about TTIP")
result_knowledge2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_knowledge2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_knowledge2)

data_americanism2 = subset(data, americanism==0) # (subsetlabel: "Negative US view")
result_americanism2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_americanism2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_americanism2)

data_americanism1 = subset(data, americanism==1) # (subsetlabel: "Positive US view")
result_americanism1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_americanism1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_americanism1)


###Appendix

##Table A1: Descriptive statistics (for robustness tests)
summary(data$high_school)
summary(data$knowledge)
summary(data$americanism)
summary(data$uni_educ)
summary(data$opp_bundestag)
summary(data$center)
summary(data$eu_pro)
summary(data$right)
summary(data$order)
summary(data$round)


##Figure A1: AMCE by university education (results are in the file "uni_educ.txt")
data_uni1 = subset(data, uni_educ==1) # (subsetlabel: "High education")
result_uni1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_uni1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_uni1)

data_uni2 = subset(data, uni_educ==0) # (subsetlabel: "Low education")
result_uni2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_uni2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_uni2)


##Figure A2: AMCE by employment sector (results are in the file "sector.txt")
data_sector1 = subset(data, sector==1) # (subsetlabel: "Manufacture")
result_sector1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_sector1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_sector1)

data_sector2 = subset(data, sector==2) # (subsetlabel: "Non-traded")
result_sector2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_sector2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_sector2)

data_sector3 = subset(data, sector==3) # (subsetlabel: "Agriculture")
result_sector3 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_sector3, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_sector3)


##Figure A3: AMCE by party support 1 (results are in opp_bundestag.txt) - checked
data_opp_bundestag1 = subset(data, opp_bundestag==1) # (subsetlabel: "Opposition parties in the Bundestag")
result_opp_bundestag1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_opp_bundestag1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_opp_bundestag1)

data_opp_bundestag2 = subset(data, opp_bundestag==0) # (subsetlabel: "Other parties")
result_opp_bundestag2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_opp_bundestag2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_opp_bundestag2)


##Figure A4: AMCE by party support 2  (results are in the file "center.txt")
data_center1 = subset(data, center==1) # (subsetlabel: "CDU/CSU and SPD")
result_center1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_center1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_center1)

data_center2 = subset(data, center==0) # (subsetlabel: "Other parties")
result_center2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_center2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_center2)


##Figure A5: AMCE by EU support (results are in the file "eu_pro.txt")
data_eu1 = subset(data, eu_pro==1) # (subsetlabel: "EU support")
result_eu1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_eu1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_eu1)

data_eu2 = subset(data, eu_pro==0) # (subsetlabel: "No EU support")
result_eu2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_eu2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_eu2)


##Figure A6: AMCE by political ideology (results are in the file "right.txt")
data_right1 = subset(data, right==1) # (subsetlabel: "Right")
result_right1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_right1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_right1)

data_right2 = subset(data, right==0) # (subsetlabel: "Left")
result_right2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_right2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_right2)


##Figure A7: AMCE by weights (results are in the file "chosen.txt", see subsetlabel: "With weights")
result_weighted = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines, weights="e_weights")
summary(result_weighted)


##Figure A8: AMCE for each round (results are in the file "round.txt")
data_round1 = subset(data, round==1) # (subsetlabel: "Round 1")
result_round1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_round1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round1)

data_round2 = subset(data, round==2) # (subsetlabel: "Round 2")
result_round2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_round2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round2)

data_round3 = subset(data, round==3) # (subsetlabel: "Round 3")
result_round3 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_round3, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round3)

data_round4 = subset(data, round==4) # (subsetlabel: "Round 4")
result_round4 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_round4, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round4)


##Figure A9: AMCE for each panel order (results are in the file "order.txt")
data_order1 = subset(data, order==1) # (subsetlabel: "Left panel")
result_round1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_order1, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round1)

data_order2 = subset(data, order==2) # (subsetlabel: "Right panel")
result_round2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement, data=data_order2, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_round2)


##Table A4: Randomization test
#Dimension: Customs
data$customs = as.factor(data$customs)
data$customs2 = relevel(data$customs, ref = "remain as they are")
rob_customs = multinom(customs2 ~ age +  high_school + income + women, data = data)
summary(rob_customs)
coeftest(rob_customs)

#Dimension: Standards
data$standards = as.factor(data$standards)
data$standards2 = relevel(data$standards, ref = "remain as they are")
rob_standards = multinom(standards2 ~ age + high_school + income + women, data = data)
summary(rob_standards)
coeftest(rob_standards)

#Dimension: Exceptions 
data$exceptions = as.factor(data$exceptions)
data$exceptions2 = relevel(data$exceptions, ref = "no exceptions")
rob_exceptions = multinom(exceptions2 ~ age +  high_school + income + women, data = data)
summary(rob_exceptions)
coeftest(rob_exceptions)

#Dimension: Dispute settlement
data$dispute = as.factor(data$dispute)
data$dispute2 = relevel(data$dispute, ref = "domestic court")
rob_dispute = multinom(dispute2 ~ age + high_school + income + women, data = data)
summary(rob_dispute)
coeftest(rob_dispute)

#Dimension: Endorsement
data$endorsement = as.factor(data$endorsement)
data$endorsement2 = relevel(data$endorsement, ref = "European Commission")
rob_endorsement = multinom(endorsement2 ~ age + high_school + income + women, data = data)
summary(rob_endorsement)
coeftest(rob_endorsement)


##Figure A10: Conjoint analysis using rating of treaties
result_rating = amce(rating ~ customs + standards + exceptions + dispute + endorsement + dispute, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_rating)


##Table A5: Average Component Interaction Effects (ACIE)

#Interaction effects: dispute settlement mechanism and customs
result_int1 = amce(selected ~ customs + standards + exceptions + dispute + endorsement + dispute*customs, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_int1)

#Interaction effects: dispute settlement mechanism and standards
result_int2 = amce(selected ~ customs + standards + exceptions + dispute + endorsement + dispute*standards, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_int2)

#Interaction effects: dispute settlement mechanism and exceptions
result_int3 = amce(selected ~ customs + standards + exceptions + dispute + dispute*exceptions + endorsement, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_int3)

#Interaction effects: dispute settlement mechanism and endorsement
result_int4 = amce(selected ~ customs + standards + exceptions + dispute + dispute*endorsement, data=data, design=ttipdesign, respondent.id="id_g", cluster=T, baselines=baselines)
summary(result_int4)
